package com.dawson.gitosc.ui;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;

import com.dawson.gitosc.R;

/**
 * AppCompatActivity支持：Fargment
 */
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    final String DRAWER_MENU_TAG = "drawer_menu";
    private ActionBar mActionBar;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    private DrawerNavigationMenu mMenu = DrawerNavigationMenu.newInstance();//新的抽屉侧滑菜单UI
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView(savedInstanceState);
    }

    private void initView(Bundle savedInstanceState) {
        mActionBar = getSupportActionBar();
        if (mActionBar != null) {
            //导航栏图标可以点击
            mActionBar.setDisplayHomeAsUpEnabled(true);
            mActionBar.setHomeButtonEnabled(true);
        }
        //侧滑抽屉视图
        mDrawerLayout= (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerLayout.setDrawerListener(new mDrawerListener());
        //完成ActionBar与DrawerLayout的关联函数
        //ActionBarDrawerToggle是一个开关，用于打开/关闭DrawerLayout抽屉
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, null, 0, 0);
        if (null == savedInstanceState) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.main_slidingmenu_frame, mMenu, DRAWER_MENU_TAG).commit();
        }

    }
    /**Drawer侧滑同步指示器*/
    @Override
    public void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }
    /**mDrawerToggle点击动作展开Drawer*/
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        return mDrawerToggle.onOptionsItemSelected(item);
    }

    /**
     * Drawer侧滑抽屉监听事件设置mDrawerToggle的状态
     */
    private class mDrawerListener implements DrawerLayout.DrawerListener{
        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            mDrawerToggle.onDrawerSlide(drawerView, slideOffset);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            Log.i(TAG,"DraweOpened打开");
            mDrawerToggle.onDrawerOpened(drawerView);
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            Log.i(TAG,"DraweClosed关闭");
            mDrawerToggle.onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerStateChanged(int newState) {
            mDrawerToggle.onDrawerStateChanged(newState);
        }
    }
}
